import { view } from "cc";
import * as fgui from "fairygui-cc";
import Instance from "../../../Core/Utils/Instance";
import IUIWindow from "./IUIWindow";
import WindowObject from "./WindowObject";
import { MainView } from "../scene/main/MainView";

export default class ModuleWindow {
    private _windowOpenList: Array<IUIWindow>;
    private _windowDict: Object;
    public static get ins(): ModuleWindow {
        return Instance.get(ModuleWindow);
    }
    public init(): void {
        this._windowOpenList = [];
        this._windowDict = {};

    }
    public get mainView(): MainView {
        for (let i of this._windowOpenList) {
            if (i.view.name == WindowObject.ins.MainView) {
                return i as MainView;
            }
        }
    }
    public removeWindow(window: any): void {
        for (let i = 0; i < this._windowOpenList.length; i++) {
            if (!window || !window.view) {
                console.log(window.view);
            }
            if (!this._windowOpenList[i] || !this._windowOpenList[i].view) {
                console.log(this._windowOpenList[i].view);
            }
            if (window.view.name == this._windowOpenList[i].view.name) {
                this._windowOpenList.splice(i, 1);
                break;
            }
        }
        delete this._windowDict[window.view.name];
    }
    public getisOpen(windowClass: any): boolean {
        for (let i of this._windowOpenList) {
            if (i.view.name == windowClass) {
                return true;
            }
        }
        return false;
    }
    /**打开弹窗 */
    public openWindow(windowClass: any, ...arg: any): void {
        this.showWindow(windowClass, true, false, false, true, arg);
    }
    /**打开*/
    public openView(windowClass: any, ...arg: any): void {
        this.showWindow(windowClass, false, true, false, false, arg);
    }
    /**
    * 显示窗体
    */
    private showWindow(windowClass: string, modal: boolean = false, closeOthers: boolean = false, closeWhenOpend: boolean = false, touchClose: boolean = false, args?: Array<any>): void {
        console.log(`显示窗体:${windowClass}`);
        if (this.getisOpen(windowClass)) {
            console.log(`${windowClass}已开启，不创建`);
            return;
        }
        // 判断缓存中是否存在
        let window: IUIWindow = this._windowDict[windowClass];
        if (window == null) {
            window = new WindowObject.ins.windowMap[windowClass]();
            this._windowDict[windowClass] = window;
        } else {
            console.log(`${windowClass}已存在，不创建`);
        }
        window.isShowCenter = modal;
        window.loadRes(() => {
            if (!window.isInited) {
                window.init();
                window.view.name = windowClass;
            }
            if (closeWhenOpend && window.isShow) {
                window.close();
                return;
            } else {
                if (closeOthers) {
                    this.closeAllWindows();
                }
                window.show(modal, touchClose, args);
                this._windowOpenList.push(window);
                // 打开模式
                if (window.isShowCenter ) {//   
                    window.view.width = window.view.sourceWidth;
                    window.view.height = window.view.sourceHeight;
                    const windowWidth: number = window.view.sourceWidth;
                    const windowHeight: number = window.view.sourceHeight;
                    const widthH: number = view.getVisibleSize().width;
                    const heightH: number = view.getVisibleSize().height;
                    const x = (widthH - windowWidth) >> 1;
                    const y = (heightH - windowHeight) >> 1;
                    window.position(x, y);
                } else {
                    window.position(0, 0);
                }
            }
        })
    }
    /**
     * 关闭所有窗体
     */
    public closeAllWindows(force: boolean = false): void {
        var windowList: Array<IUIWindow> = this._windowOpenList.concat();
        for (var window of windowList) {
            if (window.notClose && !force) {
                continue;
            }
            if (!window.isShow) {
                continue;
            }
            console.log(`关闭:`, window);
            window.close();
        }
    }
    /**
     * 关闭指定窗体
     */
    public closeWindow(windowClass: string): void {
        var window: IUIWindow = this._windowDict[windowClass];
        var windowList: Array<IUIWindow> = this._windowOpenList.concat();
        for (var window of windowList) {
            if (window.view.name == windowClass) {
                window.close();
                break;
            }
        }
    }
}